package com.uk.ui.manaxhim;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.uk.HandleMessages;
import com.uk.data.entities.Parameters;
import com.uk.data.entities.User;
import com.uk.exceptions.NestedException;
import com.vaadin.data.Item;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormFieldFactory;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickListener;

public class ParamsForm extends Form implements ClickListener{

	private ManaxhimPanel manaxhimPanel;
	
	private Button save = new Button("Ruaj", (ClickListener) this);
	private Button edit = new Button("Ndrysho", (ClickListener) this);
	
	private TextArea textArea = new TextArea("Vlera");
	
	public ParamsForm(ManaxhimPanel manaxhimPanel) {
		super();
		this.manaxhimPanel = manaxhimPanel;
		
		setWriteThrough(false);
		this.setInvalidCommitted(false);
		HorizontalLayout footer = new HorizontalLayout();
		footer.setSpacing(true);
		footer.addComponent(save);
		footer.addComponent(edit);
		footer.setVisible(false);
		setFooter(footer);
		
		setFormFieldFactory(new DefaultFieldFactory() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Field createField(Item item, Object propertyId, Component uiContext) {
				Field field = super.createField(item, propertyId, uiContext);
				
				if (propertyId.equals("param")){
					
					TextField tf = (TextField)field;
					tf.setMaxLength(30);
					tf.setWidth("200px");
					tf.setReadOnly(true);
					
					return tf;
				}
				
				
				if (propertyId.equals("value")){
					textArea.setMaxLength(1000);
					textArea.setWidth("200px");
					textArea.setColumns(50);
					textArea.setRows(8);
					textArea.setNullRepresentation("");
					textArea.setImmediate(true);
					return textArea;
					
				}
	
				return null;
			}
		});
				
	}

	
	
	
	
	@Override
	public void buttonClick(ClickEvent event) {
		Button source = event.getButton();
		this.setComponentError(null);
		try {
			if (source == edit){
				setReadOnly(false);
				
				
			}
			else if (source == save){
				try {
					setReadOnly(false);
					commit();
					
					//User user = null;
					//user = manaxhimPanel.getFaturaBean().updateUser(((BeanItem<User>)this.getItemDataSource()).getBean());
					((BeanItem<Parameters>)this.getItemDataSource()).getBean().setValue(textArea.getValue().toString());
					setReadOnly(true);
				} catch (NestedException e) {
					HandleMessages.showErrorOnScreen(e, this);
					
				}
			}
		} catch (Exception e) {
			HandleMessages.showErrorOnScreen(e, this);
			
		}
		
	}





	@Override
	public void setItemDataSource(Item newDataSource, Collection<?> propertyIds) {
		this.setVisible(true);
		if (newDataSource != null){
			setReadOnly(false);
			List<Object> orderedProperties = Arrays.asList(new Object[]{"param", "value"});
			super.setItemDataSource(newDataSource, orderedProperties);
			setReadOnly(true);
			getFooter().setVisible(true);
		}
		else{
			super.setItemDataSource(null);
			this.setVisible(false);
			getFooter().setVisible(false);
		}
	}





	@Override
	public void setReadOnly(boolean readOnly) {
		super.setReadOnly(readOnly);
		this.textArea.setReadOnly(readOnly);
		save.setVisible(!readOnly);
		edit.setVisible(readOnly);
		
	}





	
	
}
